package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.dao.PurchaseReturnDetailsMapper;
import com.spark.incoicings.dao.PurchaseReturnOrderMapper;
import com.spark.incoicings.service.PurchaseReturnOrderService;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.entity.PurchaseReturnDetails;
import com.spark.invoicings.entity.PurchaseReturnOrder;
import com.spark.invoicings.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseReturnOrderServiceImpl extends ServiceImpl<PurchaseReturnOrderMapper, PurchaseReturnOrder> implements PurchaseReturnOrderService {

    @Resource
    private PurchaseReturnOrderMapper purchaseReturnOrderMapper;

    @Resource
    private PurchaseReturnDetailsMapper purchaseReturnDetailsMapper;

    @Resource
    private ProductMapper productMapper;

    /**
     * 更新采购退货单
     * @param purchaseReturnOrder
     * @return
     */
    @Transactional
    @Override
    public Result updatePurchaseReturnOrder(PurchaseReturnOrder purchaseReturnOrder) {
        // 应退还总金额
        double totalRefundAmount = 0;
        // 已退还总数
        int totalReturnedQty = 0;
        for (PurchaseReturnDetails purchaseReturnDetails : purchaseReturnOrder.getPurchaseReturnDetailsList()) {
            totalRefundAmount += purchaseReturnDetails.getRefundAmount();
            totalReturnedQty += purchaseReturnDetails.getReturnQuantity();
            // 计算应退款金额
            purchaseReturnDetails.setRefundAmount
                    (purchaseReturnDetails.getReturnQuantity() * purchaseReturnDetails.getReturnUnitPrice());
            purchaseReturnDetailsMapper.updateById(purchaseReturnDetails);

            // 减少库存
            if (purchaseReturnOrder.getReturnStatusId() == 134) {
                Product product = productMapper.selectProductById(purchaseReturnDetails.getProductId());
                product.setCurrentInventory(product.getCurrentInventory() - purchaseReturnDetails.getReturnQuantity());
                productMapper.updateById(product);
            }
        }
        purchaseReturnOrder.setTotalRefundAmount(totalRefundAmount);
        purchaseReturnOrder.setTotalReturnedQty(totalReturnedQty);
        purchaseReturnOrderMapper.updateById(purchaseReturnOrder);
        return Result.success(200, "success", purchaseReturnOrder.getPurchaseReturnOrderId());
    }

    /**
     * 保存采购退货单
     * @param purchaseReturnOrder
     * @return
     */
    @Transactional
    @Override
    public Result addPurchaseReturnOrder(PurchaseReturnOrder purchaseReturnOrder) {
        // 应退还总金额
        double totalRefundAmount = 0;
        // 已退还总数
        int totalReturnedQty = 0;
        for (PurchaseReturnDetails purchaseReturnDetails : purchaseReturnOrder.getPurchaseReturnDetailsList()) {
            totalRefundAmount += purchaseReturnDetails.getRefundAmount();
            totalReturnedQty += purchaseReturnDetails.getReturnQuantity();
            // 计算应退款金额
            purchaseReturnDetails.setRefundAmount
                    (purchaseReturnDetails.getReturnQuantity() * purchaseReturnDetails.getReturnUnitPrice());
            purchaseReturnDetailsMapper.insert(purchaseReturnDetails);

            // 减少库存
            if (purchaseReturnOrder.getReturnStatusId() == 134) {
                Product product = productMapper.selectProductById(purchaseReturnDetails.getProductId());
                product.setCurrentInventory(product.getCurrentInventory() - purchaseReturnDetails.getReturnQuantity());
                productMapper.updateById(product);
            }
        }
        purchaseReturnOrder.setTotalRefundAmount(totalRefundAmount);
        purchaseReturnOrder.setTotalReturnedQty(totalReturnedQty);
        purchaseReturnOrderMapper.insert(purchaseReturnOrder);
        for (PurchaseReturnDetails purchaseReturnDetails : purchaseReturnOrder.getPurchaseReturnDetailsList()) {
            purchaseReturnDetails.setPurchaseReturnOrderId(purchaseReturnOrder.getPurchaseReturnOrderId());
            purchaseReturnDetailsMapper.updateById(purchaseReturnDetails);
        }
        return Result.success(200, "success", purchaseReturnOrder.getPurchaseReturnOrderId());
    }

    /**
     * 根据id查询采购退货单
     * @return
     */
    @Override
    public PurchaseReturnOrder selectPurchaseReturnOrderById(Integer id) {
        return purchaseReturnOrderMapper.selectPurchaseReturnOrderById(id);
    }

    /**
     * 查询所有采购退货单
     * @return
     */
    @Override
    public List<PurchaseReturnOrder> selectPurchaseReturnOrderAll(Map<String, Object> map) {
        return purchaseReturnOrderMapper.selectPurchaseReturnOrderAll(map);
    }
}
